package com.java.leetcode;

/**
 * @Author ynb
 * 最长公共前缀
 * @Date 2021/5/26 15:12
 * @Version 1.0
 */
public class LeetCode014 {
    public static String longestCommonPrefix(String[] strs) {
        //首先判strs为空和strs只有一个元素的情况
        if (strs.length == 0){
            return "";
        }
        if (strs.length==1){
            return  strs[0];
        }
        //排除上面两种情况后，遍历数组，然后依次判断是否存在。
        String ans=strs[0];
        for(String s:strs ){
            while (!s.startsWith(ans)){
                ans=ans.substring(0,ans.length()-1);
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        String [] strs ={"flower","flow","flight"};
        System.out.println(longestCommonPrefix(strs));
    }
}
